<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="用户id" prop="userId">
        <el-input v-model="queryParams.userId" placeholder="请输入用户id" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="账户id" prop="accountId">
        <el-input v-model="queryParams.accountId" placeholder="请输入账户id" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="产品id" prop="productId">
        <el-input v-model="queryParams.productId" placeholder="请输入产品id" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="handleAdd"
          v-hasPermi="['finace:usd_rmb:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate"
          v-hasPermi="['finace:usd_rmb:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['finace:usd_rmb:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="Download" @click="handleExport"
          v-hasPermi="['finace:usd_rmb:export']">导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="usd_rmbList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="交易唯一标识" align="center" prop="transactionId" />
      <el-table-column label="用户id" align="center" prop="userId" />
      <el-table-column label="账户id" align="center" prop="accountId" />
      <el-table-column label="产品id" align="center" prop="productId" />
      <el-table-column label="提交时间" align="center" prop="submitTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.submitTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="确认时间" align="center" prop="confirmDate" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.confirmDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="交易类型" align="center" prop="type" />
      <el-table-column label="汇率" align="center" prop="price" />
      <el-table-column label="美元数" align="center" prop="quantity" />
      <el-table-column label="RMB额度" align="center" prop="amount" />
      <el-table-column label="手续费" align="center" prop="fee" />
      <el-table-column label="价格日期" align="center" prop="priceDate" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.priceDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="与哪个账户交易" align="center" prop="transWith" />
      <el-table-column label="注释" align="center" prop="comment" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['finace:usd_rmb:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"
            v-hasPermi="['finace:usd_rmb:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改美元理财对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="usd_rmbRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="用户id" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入用户id" />
        </el-form-item>
        <el-form-item label="账户id" prop="accountId">
          <el-input v-model="form.accountId" placeholder="请输入账户id" />
        </el-form-item>
        <el-form-item label="产品id" prop="productId">
          <el-input v-model="form.productId" placeholder="请输入产品id" />
        </el-form-item>
        <el-form-item label="提交时间" prop="submitTime">
          <el-date-picker clearable v-model="form.submitTime" type="date" value-format="YYYY-MM-DD"
            placeholder="请选择提交时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="确认时间" prop="confirmDate">
          <el-date-picker clearable v-model="form.confirmDate" type="date" value-format="YYYY-MM-DD"
            placeholder="请选择确认时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="汇率" prop="price">
          <el-input v-model="form.price" placeholder="请输入汇率" />
        </el-form-item>
        <el-form-item label="美元数" prop="quantity">
          <el-input v-model="form.quantity" placeholder="请输入美元数"  @input="onQuantityInput"/>
        </el-form-item>
        <el-form-item label="RMB额度" prop="amount">
          <el-input v-model="form.amount" placeholder="请输入RMB额度" @input="onAmountInput" />
        </el-form-item>
        <el-form-item label="手续费" prop="fee">
          <el-input v-model="form.fee" placeholder="请输入手续费" />
        </el-form-item>
        <el-form-item label="价格日期" prop="priceDate">
          <el-date-picker clearable v-model="form.priceDate" type="date" value-format="YYYY-MM-DD"
            placeholder="请选择价格日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="与哪个账户交易" prop="transWith">
          <el-input v-model="form.transWith" placeholder="请输入与哪个账户交易" />
        </el-form-item>
        <el-form-item label="注释" prop="comment">
          <el-input v-model="form.comment" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Usd_rmb" lang="ts">
import { listUsd_rmb, getUsd_rmb, delUsd_rmb, addUsd_rmb, updateUsd_rmb } from "@/app/finace/api/finace/usd_rmb";

const { proxy } = getCurrentInstance();

import {
  data,
  open,
  loading,
  showSearch,
  ids,
  single,
  multiple,
  total,
  title,
  userStore,
  userInfo,
  permissions,
  usd_rmbList,
} from './data'
import { getCurrentInstance, toRefs } from "vue";
import { copySelectionToForm } from "./utils";
import {onQuantityInput,
  onAmountInput} from './events'
const { queryParams, form, rules } = toRefs(data);

/** 查询美元理财列表 */
function getList() {
  loading.value = true;
  listUsd_rmb(queryParams.value).then(response => {
    usd_rmbList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    transactionId: null,
    userId: null,
    accountId: null,
    productId: null,
    submitTime: null,
    confirmDate: null,
    type: null,
    price: null,
    quantity: null,
    amount: null,
    fee: null,
    priceDate: null,
    transWith: null,
    comment: null
  };
  proxy.resetForm("usd_rmbRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.transactionId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
  // jm add 
  if (selection.length === 1) {
    copySelectionToForm(selection)
  }
  else {
    reset()
  }
}

/** 新增按钮操作 */
function handleAdd() {
  // reset();
  form.value.transactionId = null;
  open.value = true;
  title.value = "添加美元理财";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _transactionId = row.transactionId || ids.value
  getUsd_rmb(_transactionId).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改美元理财";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["usd_rmbRef"].validate(valid => {
    if (valid) {
      if (form.value.transactionId != null) {
        updateUsd_rmb(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addUsd_rmb(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _transactionIds = row.transactionId || ids.value;
  proxy.$modal.confirm('是否确认删除美元理财编号为"' + _transactionIds + '"的数据项？').then(function () {
    return delUsd_rmb(_transactionIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => { });
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('finace/usd_rmb/export', {
    ...queryParams.value
  }, `usd_rmb_${new Date().getTime()}.xlsx`)
}

getList();
</script>
